<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写sleep函数</title>
</head>
<body>
    <script>
        // sleep 休息一下
        // then
        function sleep(n) {
            let p;
            p  = new Promise((resolve, reject) => {
                setTimeout(() => {
                    // pending 等待
                    console.log(p,'////');
                    // resolve();
                    reject();// 将 Promise 对象 p 的状态从 pending 改变为 rejected。
                    // fulfilled  成功
                    // rejected  失败
                    console.log(p);
                }, n);
            });
            return p;
        }
        sleep(3000)
        .then(() => {
            console.log('//////');
        })
        .catch(() => {
            console.log('error');
        })
        .finally(() => {
            // promise 状态从 pending 到了 fulfilled 或 rejected
            console.log('finally');
        })
    </script>
</body>
</html>